/**
 *  mobile-form组件
 *  提供方便的form表单布局功能
 *  表单校验
 *  基本表单控件的初始化
 *  使用data-role="form"进行表单的自动初始化处理
 */

(function($,undefined){
	
	//form组件调用接口
	 $.fn.mobileForm=function(params,successCallback,failCallback){
		 var formid=$(this).attr("id");
		 if(!formid) return;
		 $("#"+formid).addClass("form-horizontal");
		 $("#"+formid).renderForm(formid,params);
		 $("#"+formid).validateForm(formid,successCallback,failCallback);
	 };
	 
	 
	 //默认渲染方法
	 $.fn.renderForm=function(id,p){
		 
          $("input",$("#"+id)).each(function(index,dom){
		       	  var label=$(dom).attr("label");
		       	  if(label){
			       	  $(dom).wrap('<div class="control-group "><div class="controls "></div></div>');
			       	  $(dom).parent().before('<label class="control-label ">'+label+'</label>');
		       	  }
		 });
          $("select",$("#"+id)).each(function(index,dom){
	       	  var label=$(dom).attr("label");
		       	if(label){
		       	  $(dom).wrap('<div class="control-group"><div class="controls "></div></div>');
		       	  $(dom).parent().before('<label class="control-label ">'+label+'</label>');
		       	  $(dom).mobileSelect();
		       	}
		            
	     });
          $("textarea",$("#"+id)).each(function(index,dom){
	       	  var label=$(dom).attr("label");
		       	if(label){
		       	  $(dom).wrap('<div class="control-group"><div class="controls "></div></div>');
		       	  $(dom).parent().before('<label class="control-label ">'+label+'</label>');
		       	}        
	     });
          $("label.checkbox",$("#"+id)).each(function(index,dom){
//        	     console.log(dom);
        	     var label=$(dom).attr("label");
        	  	if(label){
  		       	  $(dom).wrap('<div class="control-group"><div class="controls "></div></div>');
  		       	  $(dom).parent().before('<label class="control-label ">'+label+'</label>');
  		       	}      
        	     $(">input",$(dom)).mobileCheckbox();
          });
          $("label.radio",$("#"+id)).each(function(index,dom){
//     	     console.log(dom);
     	     if(!$(dom).parent(".form-group").length){
     	    	  var label=$(dom).attr("label");
     	     	if(label){
  		       	  $(dom).wrap('<div class="control-group"><div class="controls "></div></div>');
  		       	  $(dom).parent().before('<label class="control-label ">'+label+'</label>');
  		       	}      
     	     }
     	     $(">input",$(dom)).mobileRadio();
          });
          $("div.switch",$("#"+id)).each(function(index,dom){
      	     console.log(dom);
      	     var label=$(dom).attr("label");
      	 	 if(label){
		       	  $(dom).wrap('<div class="control-group"><div class="controls "></div></div>');
		       	  $(dom).parent().before('<label class="control-label ">'+label+'</label>');
		       	}      
      	     $(dom).mobileSwitch();
           });
          
          $("div[class*='form-group']",$("#"+id)).each(function(index,dom){
        	  var label=$(dom).attr("label");
		       	if(label){
		       	  $(dom).wrap('<div class="control-group"><div class="controls "></div></div>');
		       	  $(dom).parent().before('<label class="control-label ">'+label+'</label>');
		       	}        
          })
          $("span.text",$("#"+id)).each(function(index,dom){
	       	  var label=$(dom).attr("label");
	       	  if(label){
		       	  $(dom).wrap('<div class="control-group "><div class="controls "></div></div>');
		       	  $(dom).parent().before('<label class="control-label ">'+label+'</label>');
	       	  }
	 });
          
		 
	 };
	
	/**
	* 校验表单
	*/
	$.fn.check = function(){
		return $(this).validate().form();
	};
    	 
	
	 //表单校验提示
	 $.fn.validateForm = function(){
		 var form_id = $(this).attr("id");
		 if(!form_id) return;
		 var v = $("#"+form_id).validate({
            errorPlacement: function (lable, element) {
			     var errorText=lable.html();
			     if(errorText&&errorText!="")
			     $("#valError-"+form_id).mobileMsgBox({msgText:errorText,delayTime:1500});
            },
            success: function (lable) {
            	return false;
            }
        });
	 }
	 
	 //获取form表单值集合
	 $.fn.values = function() {
			var formParams = {};
			var params = $(this).formToArray();

			for ( var i = 0; i < params.length; i++) {
				var key = params[i].name;
				if(formParams[key]){
					formParams[key] += ","+params[i].value;
				}else{
					formParams[key] = params[i].value;
				}
			}
			return formParams;
		};
	   
		//自动初始化form组件
		$.mobile.document.bind('pagecreate create', function (e) {
			
			$(':jqmData(role="form")',$(e.target)).each(function () {
				var $form = $(this);
				$form.mobileForm();
			});
		});
	
})(jQuery);